<template>
  <div class="login">
    <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="0px" class="login-bok">
      <h3 class="login-title">欢迎 登录</h3>
        <div @click="toAdmin" class="adminLogin">管理员登录👉</div>

      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="账号">
          <i slot="prepend" class="el-icon-s-custom"/>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" placeholder="密码" v-model="loginForm.password">
          <i slot="prepend" class="el-icon-lock"/>
        </el-input>
      </el-form-item>

      <el-form-item prop="code">
        <el-row :span="24">
          <el-col :span="12">
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="请输入验证码" size=""></el-input>
          </el-col>
          <el-col :span="12">
            <!--验证码组件-->
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item>
        <div class="login-btn">
          <el-button type="primary"
                     style="margin-left: auto;width: 55%"
                     @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </div>
        <el-form-item style="width: 100%">
          <a href="/register" class="register">注册</a>
        </el-form-item>
      </el-form-item>

    </el-form>


  </div>
</template>

<script>

import Cookies from "js-cookie";
import {encrypt,decrypt} from "../utils/jsencrypt";
import {getCodeImg} from "../api/login";
import {removeToken} from "../utils/auth";


export default {
  name: "login",
  data() {
    return {
      url:"http://localhost:81/",
      codeUrl: "",
      loginForm: {
        username: "18385928322",
        password: "12345",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      // 校验
      rules: {
        username:
          [
            {required: true, message: "请输入用户名", trigger: "blur"}
          ],
        password: [{required: true, message: "请输入密码", trigger: "blur"}],
        code: [{required: true, message: "请输入验证码", trigger: "blur"}]
      },
      loading: false,
    }
  },
  created() {
    this.getCode()
  },
  methods: {

    toAdmin(){
      console.log("555")
      window.location.href = this.url;
    },

    //获取验证码
    getCode() {
      getCodeImg().then(res => {
        console.log(res)
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path:"/index" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }

  },

}


</script>

<style rel="stylesheet/scss" lang="scss">
.login{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login.jpg");
  background-size: cover;
}
.adminLogin{
  text-align: right;
  font-size: 10px;
  margin-bottom: 10px;
  cursor:pointer
}
.login-bok {
  width: 30%;
  float: right;
  margin: 150px auto;
  border: 1px solid #DCDFE6;
  padding: 20px;
  border-radius: 10px;
  background: #ffffff;
  box-shadow: 0 0 30px #DCDFE6;
}

.login-title {
  text-align: center;
  margin: 0 auto 20px auto;
  color: #303133;
}

.register {
  float: right;
  background: #ffffff;
  color: rgb(24, 144, 255);
}

.login-code {
  width: 55%;
  height: 38px;
  margin-left: 20px;

  img {
    width: 100%;
    cursor: pointer;
    vertical-align: middle;
  }

}
</style>

